package Model;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import zOthers.ImageStructure;
import zOthers.MyStory;

public class ModelPanelStartMenu extends AbstractModel {

	public static final Integer SCREEN_ID_BASIC = 0;
	public static final Integer SCREEN_ID_LEVEL = 1;
	public static final Integer SCREEN_ID_OPTIONS = 2;

	private Integer screenID;
	private Integer selectedMapIndex;
	private BufferedImage imageBackground;	// Background image
	private Cursor cursor;
	private Cursor cursorLeft;
	private Cursor cursorRight;
	private Point mousePosition;
	private boolean isModelInitialized;	// This flag will be set to true when initialization is finished
	private Integer panelStartMenuWidth;
	private Integer panelStartMenuHeight;

	/*
	 * Constructor
	 */
	public ModelPanelStartMenu() {
		isModelInitialized = false;
	}

	/*
	 * This method is only called when the view is visualized for the first time
	 */
	public void modelInitialization() {
		setImageBackground(MyStory.imageLoader.getImageByName(ImageStructure.IMAGE_NAME_PANEL_LOGIN_BACKGROUND));
		setPanelStartMenuWidth(800);
		setPanelStartMenuHeight(700);
		createCursors();
		setSelectedMapIndex(0);
		setIsModelInitialized(true);
		setScreenID(ModelPanelStartMenu.SCREEN_ID_BASIC);
	}

	/*
	 * Create custom cursors
	 */
	private void createCursors() {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Point cursorHotSpot = new Point(0,16);
	}

	/*
	 * Mutators
	 */
	public void setPanelStartMenuWidth(Integer pMW) {
		System.out.println(this.getClass().toString() + ".setPanelStartMenuWidth() called");
		Integer OldPanelStartMenuWidth;
		if(panelStartMenuWidth == null)
			OldPanelStartMenuWidth = -1;
		else
			OldPanelStartMenuWidth = panelStartMenuWidth;
		panelStartMenuWidth = pMW;
		firePropertyChange("panelStartMenuWidth", OldPanelStartMenuWidth, panelStartMenuWidth);
	}

	public void setPanelStartMenuHeight(Integer pMH) {
		Integer OldPanelStartMenuHeight;
		if(panelStartMenuHeight == null)
			OldPanelStartMenuHeight = -1;
		else
			OldPanelStartMenuHeight = panelStartMenuHeight;
		panelStartMenuHeight = pMH;
		firePropertyChange("panelStartMenuHeight", OldPanelStartMenuHeight, panelStartMenuHeight);
	}

	public void setIsModelInitialized(boolean iMI) {
		boolean oldIsModelInitialized = isModelInitialized;
		isModelInitialized = iMI;
		firePropertyChange("isModelInitialized", oldIsModelInitialized, isModelInitialized);
	}

	public void setScreenID(Integer sId) {
		Integer oldScreenID;
		if(screenID == null)
			oldScreenID = -1;
		else
			oldScreenID = screenID;
		screenID = sId;
		firePropertyChange("screenID", oldScreenID, screenID);
	}

	public void setSelectedMapIndex(Integer mI) {
		Integer oldSelectedMapIndex;
		if(selectedMapIndex == null)
			oldSelectedMapIndex = -1;
		else
			oldSelectedMapIndex = selectedMapIndex;
		selectedMapIndex = mI;

		/*
		 * Check the size of mapsList
		 */
		if(selectedMapIndex < 0)
			selectedMapIndex = 0;

		firePropertyChange("selectedMapIndex", oldSelectedMapIndex, selectedMapIndex);
	}

	public void setImageBackground(BufferedImage iB) {
		BufferedImage oldImageBackground;
		if(imageBackground == null)
			oldImageBackground = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR);
		else
			oldImageBackground = imageBackground;
		imageBackground = iB;
		firePropertyChange("imageBackground", oldImageBackground, imageBackground);
	}

	public void setCursor(Cursor c) {
		Cursor oldCursor;
		if(cursor == null)
			oldCursor = Cursor.getDefaultCursor();
		else
			oldCursor = cursor;
		cursor = c;
		firePropertyChange("cursor", oldCursor, cursor);
	}

	public void setMousePosition(Point mP) {
		mousePosition = mP;

		/*
		 * Change cursor with given mouse position
		 */
		if(screenID == ModelPanelStartMenu.SCREEN_ID_BASIC) {
			this.setCursor(Cursor.getDefaultCursor());
			return;
		}

		if(mousePosition.getY() >= 500 || mousePosition.getY() <= 100) {
			setCursor(Cursor.getDefaultCursor());
			return;
		}

		if(mousePosition.getX() <= (panelStartMenuWidth - 200) / 2 - 20) {
			setCursor(cursorLeft);
		}
		else if(mousePosition.getX() >= (panelStartMenuWidth - 200) / 2 + 200 + 20) {
			setCursor(cursorRight);
		}
		else {
			setCursor(Cursor.getDefaultCursor());
		}
	}

	public void setExit(Integer unused){
		System.out.println("Player terminated the game.");
		System.exit(0);
	}

	/*
	 * Accessors
	 */
	public Integer getScreenID() {
		return screenID;
	}

	public Integer getSelectedMapIndex() {
		return selectedMapIndex;
	}
}
